<?php
class RegisterAction extends Action{
	
	function init(){
		$regType = SiteConfig::get('reg_type');
		if($regType == 0){
			$this->Message('网站未开发注册功能.<br /><a href="'.
					url(array($GLOBALS['METHOD']=>'Index',$GLOBALS['ACT']=>'Index')).'">返回首页</a>','用户注册');
		}
		$regRecvInfo = SiteConfig::get('sms_recv_phone');
		preg_match_all('@(\d+)\|([a-zA-Z0-9\x3c-\xff]+)@',$regRecvInfo,$m);
		if(isset($m[1]) && isset($m[2])){
			$recvList = array();
			$count = count($m[1]);
			for($i = 0; $i < $count; $i++){
				$recvList[$m[2][$i]] = $m[1][$i];
			}
			$this->assign('recvList', $recvList);
		}
		/* if($regRecvInfo){
			$recvList = array();
			if(strpos($regRecvInfo, ';')){
				$regRecvInfo = explode(';', $regRecvInfo);
				foreach($regRecvInfo as $row){
					if(strpos($row, '|')){
						$row = explode('|', $row);
						$recvList[$row[1]] = $row[0];
					}
				}
			}else if(strpos($regRecvInfo, '|')){
				$regRecvInfo = explode('|', $regRecvInfo);
				$recvList[$regRecvInfo[1]] = $regRecvInfo[0];
			}
			$this->assign('recvList', $recvList);
		} */
		$this->assign('regType', $regType);
	}
	
	function cmdIndex(){
		$returl=isset($_REQUEST['returl'])?$_REQUEST['returl']:enUrl(PATH."/index.php");
		$user = new UserModel();
		$this->assign('new_users', $user->getList(1,8));
		$this->assign('returl', $returl);
		$this->assign('title', L("REGISTERED_MEMBER"));
		$this->display("user/register.htm");
	}
	
	function cmdRegisterc(){
		$returl=isset($_REQUEST['returl'])?$_REQUEST['returl']:enUrl(PATH."/index.php");
		$username=isset($_POST['username'])?trim($_POST['username']):null;
		$usernick=isset($_POST['usernick'])?trim($_POST['usernick']):null;
		$password=isset($_POST['password'])?trim($_POST['password']):null;
		$password2=isset($_POST['password2'])?trim($_POST['password2']):null;
		$phone=isset($_POST['phone'])?trim($_POST['phone']):null;
		$email=isset($_POST['email'])?trim($_POST['email']):null;
		$sex=isset($_POST['sex'])?trim($_POST['sex']):null;
		$usermobile=isset($_POST['usermobile'])?trim($_POST['usermobile']):null;
		
		$msg=null;
		
		if ($username=='' || strlen($username)<3)$msg.='用户名不能为空或小于3字符⊙﹏⊙b汗<br />';
		if ($usernick=='' || strlen($usernick)<2)$msg.="昵称不能为空或小于2字符\(^o^)/~<br />";
		if ($password=='' || strlen($password)<6)$msg.="密码不能为空或小于6字符\O(∩_∩)O~<br />";
		if ($password2!=$password)$msg.="重复密码与密码不同\(^o^)/~<br />";
		if (!ereg("^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+", $email))$msg.="邮件地址错误！<br />";
		if (strlen($phone)!=11 || !is_numeric($phone))$msg.="手机号码必须为11位数字<br />";
		
		if (empty($msg)){
			$user=new UserModel();
			@$result=$user->Register($username, $usernick,$password,$email,$phone,$sex,$usermobile);
			if ($result=='OK'){
				$this->assign('username', $usernick);
				$this->assign('content', "你在".date('Y-m-d H:i:s')."注册成了天酷短信猫会员(<a href=\"http://sms.8eoo.com\">sms.8eoo.com</a>)<br />账号:$username<br />密码:$password<br />请妥善保管你的账号信息。");
				@MailModel::SendMail($email, '欢迎成为我们的一员', $this->view->fetch('common/email-default.htm'));
				$this->Message(L("COSR"),L("REGISTRATION_SUCCESS"),deUrl($returl),3);
			}else{
				$this->Message($result,'注册失败',str_replace('Registerc', 'Index', getUrl(false)),3);
			}
		}else{
			$this->Message($msg,'注册失败',str_replace('Registerc', 'Index', getUrl(false)),3);
		}
	}
	
	function cmdSmsRegister(){
		$regType = SiteConfig::get('reg_type');
		if(!in_array($regType, array(2,3)))exit('51');
		$SMS_KEY=SiteConfig::get('sms_key');
		
		$smmkey=isset($_REQUEST['smskey'])?$_REQUEST['smskey']:null;
		$phone=isset($_REQUEST['phone'])?$_REQUEST['phone']:null;
		$message=isset($_REQUEST['message'])?$_REQUEST['message']:null;
		$user;$pass;
		if(strlen($phone)<5)exit('0');
		if($SMS_KEY!=$smmkey)exit('2');
		$pass=substr($phone, strlen($phone)-4,4);
		$name=$message.substr($phone, strlen($phone)-8,4);
	
		switch (getPhoneType($phone)){
			case 'M':
				$mail=$phone."@139.com";
				break;
			case 'N':
				$mail=$phone."@189.com";
				break;
			case 'U':
				$mail=$phone."@139.com";
				break;
			default:
				$mail=$phone."@139.com";
				break;
		}
		$user=$phone;
		$tjid=0;
		if(stripos($message, "#")!==false){
			$tmp=explode("#", $message);
			$name=$tmp[0].substr($phone, strlen($phone)-8,4);
			if(strlen($tmp[1])!=""){
				$pass=$tmp[1];
			}
			if(isset($tmp[2])){
				$tjid=intval($tmp[2]);
			}
		}
		#Log::Write($tjid.'-'.$user.'-'.$name.'-'.$pass.'-'.$phone.'-'.$mail,'tjid');
		$u=new UserModel();
		echo $u->SmsReg($user, $name, $pass, $phone,$mail, 1,$tjid);
		}
}